<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/reg_1.css"/> 
<link rel="stylesheet" href="css/reg.css"/> 

<title>用户注册</title>
<script type="text/javascript" src="assets/js/request.js"></script>
<script type="text/javascript">
/**重新加载验证码****/
function reload() {
	document.checkCode.src = "imageCode.jsp";
}
function checkemail(email) {
	var str = email;
  //在JavaScript中，正则表达式只能使用"/"开头和结束，不能使用双引号
	var Expression = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	var objExp = new RegExp(Expression);
	if (objExp.test(str) == true) {
		return true;
	} else {
		return false;
	}
}
var opinionRequest=false;
  	function checkInput(){
		var name=document.getElementById("username").value;
		var pwd=document.getElementById("userpwd").value;
		var pwd1=document.getElementById("userpwd1").value;
		var email=document.getElementById("email").value;
		var realname=document.getElementById("realname").value;
		var code=document.getElementById("code").value;
		if(name==null||name==""){
			alert("请输入用户名！");
			document.getElementById("username").focus();
			return false;
		}
		if(pwd==null||pwd==""){
			alert("请输入密码！");
			document.getElementById("userpwd").focus();
			return false;
		}
		else{
			if(pwd.length<6||pwd.length>16){
				alert("密码格式为6-16位！");
				document.getElementById("userpwd").focus();
				return false;
			}
		}
		if(pwd1==null||pwd1==""){
			alert("请输入确认密码！");
			document.getElementById("userpwd1").focus();
			return false;
		}
		if(pwd!=null&&pwd1!=""){
			if(pwd!=pwd1){
				alert("确认密码与密码不相同！");
				document.getElementById("userpwd1").focus();
				return false;
			}
		}
        if(email==null||email==""){
			alert("请输入邮箱！");
			document.getElementById("email").focus();
			return false;
		}
		if(!checkemail(email)){
		    alert("邮箱格式不正确！");
			document.getElementById("email").focus();
			return false;
		}
        if(realname==null||realname==""){
			alert("请输入真实姓名！");
			document.getElementById("realname").focus();
			return false;
		}
		
       if(code==null||code==""){
			alert("请输入验证码！");
			document.getElementById("code").focus();
			return false;
		}
		return true;
	}
 function registerUser(){
      if(checkInput()){
          var name=document.getElementById("username").value;
          var code=document.getElementById("code").value;
          var param = "u.username="+name+"&code="+code;	
          opinionRequest=httpRequest("post","checkRegister",true,callbackFunc,param);
      }
     
    }
    //响应的回调函数 
	function callbackFunc(){
		if( opinionRequest.readyState==4 ){ 
     	 	if( opinionRequest.status == 200 ){
     	 	    var result =opinionRequest.responseText; 
     	 	    if(result=="errorCode"){
     	 	       alert("验证码输入错误，请重新输入！");
     	 	        document.getElementById("code").focus();
	         		document.getElementById("code").value="";
	         		reload();
     	 	        opinionRequest=false;
                    return false;
     	 	     } 
                 if(result=="errorUsername"){
                    alert("用户名含有非法字符，请重新输入用户名和验证码！");
                    document.getElementById("username").focus();
	         		document.getElementById("code").value="";
	         		reload();
     	 	        opinionRequest=false;
                    return false;
                 }
                  if(result=="errorHasname"){
                   alert("用户名已存在，请重新输入用户名和验证码！");
                    document.getElementById("username").focus();
	         		document.getElementById("code").value="";
	         		reload();
     	 	        opinionRequest=false;
                    return false;
                 }
                 if(result=="ok"){
                     
                    document.forms.regform.action="register";
	         		opinionRequest=false;
	        		document.forms.regform.method="post";
	        		document.forms.regform.submit();	
                 
                 }
                 
         	
			}
		}
	}
</script>
</head>


<body>
 <div class="header">
  </div>
  <div class="mainbody">
    <div class="top">
     <div id="hd" class="clearfix">

      </div>
       <div class="info"><span><a href="login.jsp" class="registerNew" title="登录"
						 onfocus="this.blur();">&nbsp;&nbsp; 登录</a>
		 </span>
      </div> 
      <div style="clear:both;"></div>
      </div>
     
      <div class="reg-wrapper2">
             <div align="center" >
                 <h2> 欢迎注册</h2>
          </div>
          <form id="regform" name="regform"  class="form-horizontal" method="post" >
           <div class="control-group">
		    <label class="control-label" for="username">用户名:</label>

		     <div class="controls" >
		       <input type="text"  value="" name="u.username" id="username" /> <span class="maroon">*</span><span class="help-inline">用户名可以由"数字/字母/中划线/下划线"组成</span>
		     </div>
		  </div>
           <div class="control-group">
		    <label class="control-label" for="password">设置密码:</label>

		     <div class="controls" >
		       <input type="password"  value="" name="u.password"  id="userpwd" /> <span class="maroon">*</span><span class="help-inline">长度为6~16位字符</span>
		     </div>
		  </div>
         <div class="control-group">
		    <label class="control-label" for="password" >确认密码:</label>

		     <div class="controls" >
		       <input type="password"  value="" name=""  id="userpwd1" /> <span class="maroon">*</span><span class="help-inline">长度为6~16位字符</span>
		     </div>
		  </div>
		  <div class="control-group">
		    <label class="control-label" for="realName">真实姓名:</label>

		     <div class="controls" >
		       <input type="text"  value="" name="u.realName" id="realname" /> <span class="maroon">*</span>
		     </div>
		  </div>
          <div class="control-group">
		    <label class="control-label" for="email">email:</label>

		     <div class="controls" >
		       <input type="text"  value="" name="u.email" id="email" /> <span class="maroon">*</span>
		     </div>
		  </div>
           <div class="control-group">
		    <label class="control-label" for="username">验证码:</label>  
		      <div class="controls" >
		       <input type="text"   name="code" id="code" /> <span class="maroon">*</span> <a href="#" onclick="reload()"><img border=0 src="imageCode.jsp" name="checkCode" height="20px"/>(换一张)</a>
		      
		     </div>
		     
		  </div>
          <div class="control-group">
             <label class="control-label" for="username"></label>
		  	<div class="controls">
                 <button type="button" class="btn-register" onclick="registerUser()"></button>
                
		  	</div>
         </div>
        </form>
      
      </div>
      <div id="ft">Copyright©2012-2015 WHUT All Rights Reserved </div>

  </div>

</body>
</html>